package com.caigua.mgp.mg.codegen.customize.xmlmapper;

import com.caigua.mgp.mg.codegen.customize.rules.CustomizeConditionalModelRules;
import com.caigua.mgp.mg.codegen.customize.xmlmapper.elements.CustomizeInsertSelectiveElementGenerator;
import com.caigua.mgp.mg.codegen.customize.xmlmapper.elements.CustomizeQueryByIdsElementGenerator;
import com.caigua.mgp.mg.codegen.customize.xmlmapper.elements.CustomizeQueryPageElementGenerator;
import com.caigua.mgp.mg.codegen.customize.xmlmapper.elements.CustomizeUpdateByPrimaryKeySelectiveElementGenerator;
import org.mybatis.generator.api.FullyQualifiedTable;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.codegen.mybatis3.xmlmapper.XMLMapperGenerator;
import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.AbstractXmlElementGenerator;
import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.InsertSelectiveElementGenerator;
import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.UpdateByPrimaryKeySelectiveElementGenerator;

import static org.mybatis.generator.internal.util.messages.Messages.getString;

/**
 * @BelongsProject: mybatis_generator_pluing
 * @BelongsPackage: com.caigua.mgp.mg.codegen.customize
 * @Author: caigua
 * @CreateTime: 2020-12-09 13:57
 * @Description: todo
 */
public class CustomizeXMLMapperGenerator extends XMLMapperGenerator {

    @Override
    protected XmlElement getSqlMapElement() {
        FullyQualifiedTable table = introspectedTable.getFullyQualifiedTable();
        progressCallback.startTask(getString(
                "Progress.12", table.toString())); //$NON-NLS-1$
        XmlElement answer = new XmlElement("mapper"); //$NON-NLS-1$
        String namespace = introspectedTable.getMyBatis3SqlMapNamespace();
        answer.addAttribute(new Attribute("namespace", //$NON-NLS-1$
                namespace));

        context.getCommentGenerator().addRootComment(answer);

        addResultMapWithoutBLOBsElement(answer);
        addResultMapWithBLOBsElement(answer);
        addExampleWhereClauseElement(answer);
        addMyBatis3UpdateByExampleWhereClauseElement(answer);
        addBaseColumnListElement(answer);
        addBlobColumnListElement(answer);
        addSelectByExampleWithBLOBsElement(answer);
        addSelectByExampleWithoutBLOBsElement(answer);
        addSelectByPrimaryKeyElement(answer);
        addDeleteByPrimaryKeyElement(answer);
        addDeleteByExampleElement(answer);
//        addInsertElement(answer);
        addInsertSelectiveElement(answer);
        addCountByExampleElement(answer);
        addUpdateByExampleSelectiveElement(answer);
        addUpdateByExampleWithBLOBsElement(answer);
        addUpdateByExampleWithoutBLOBsElement(answer);
        addUpdateByPrimaryKeySelectiveElement(answer);
        addUpdateByPrimaryKeyWithBLOBsElement(answer);
//        addUpdateByPrimaryKeyWithoutBLOBsElement(answer);

        this.addQueryPageElement(answer);
        this.addQueryByIdsElement(answer);
        return answer;
    }


    protected void addQueryPageElement(XmlElement parentElement) {
        if (((CustomizeConditionalModelRules)this.introspectedTable.getRules()).generateQueryPage()) {
            AbstractXmlElementGenerator elementGenerator = new CustomizeQueryPageElementGenerator();
            this.initializeAndExecuteGenerator(elementGenerator, parentElement);
        }

    }

    protected void addQueryByIdsElement(XmlElement parentElement) {

        if (((CustomizeConditionalModelRules)this.introspectedTable.getRules()).generateQueryByIds()) {
            AbstractXmlElementGenerator elementGenerator = new CustomizeQueryByIdsElementGenerator();
            this.initializeAndExecuteGenerator(elementGenerator, parentElement);
        }
    }

    protected void addInsertSelectiveElement(XmlElement parentElement) {
        if (introspectedTable.getRules().generateInsertSelective()) {
            CustomizeInsertSelectiveElementGenerator elementGenerator = new CustomizeInsertSelectiveElementGenerator();
            initializeAndExecuteGenerator(elementGenerator, parentElement);
        }
    }

    protected void addUpdateByPrimaryKeySelectiveElement(
            XmlElement parentElement) {
        if (introspectedTable.getRules().generateUpdateByPrimaryKeySelective()) {
            CustomizeUpdateByPrimaryKeySelectiveElementGenerator elementGenerator = new CustomizeUpdateByPrimaryKeySelectiveElementGenerator();
            initializeAndExecuteGenerator(elementGenerator, parentElement);
        }
    }
}
